$(function () {
    var a = setInterval(queryOrderState, 5000);
    // var b = setInterval(overtime, 1000);
    HOME.ajaxHOMEFn({
        url: HOME.baseUrl.pay,
        type: 'POST',
        dataType: 'json',
        data: {
            order_sn: HOME.getQueryVariable('orderId'),
            pay_type : HOME.getQueryVariable('paytype')
        }
    }, function (res) {
        alarm = new Alarm(res.countDown, function (second, minute, hour, day) { //计时钟.
            var txt =  hour + '小时' + minute + '分' + second + '秒';
            if(hour == 0){
            txt =   minute + '分' + second + '秒';
            }
            if(hour == 0&&minute == 0){
            txt =   second + '秒';
            }
            $('#timer').html(txt);
        }, function () { //倒计时结束
            // window.location.reload();
            $('.prompt_complete').show();
            $('.QR_code , .pay_complete').hide();
        });
        
        alarm.start();
        res.paymoney = HOME.getQueryVariable('paymoney') == '' ? 0 : HOME.getQueryVariable('paymoney')
        res.orderid = HOME.getQueryVariable('orderId')
        console.log(res)
        res.id=HOME.getQueryVariable('id')
        res.type=HOME.getQueryVariable('type')
        res.typeAssort=HOME.getQueryVariable('typeAssort')
        res.curId = HOME.getQueryVariable('curId')
        $('#paycode').html(template('paycodeTpl', {'res': res}));
        var windowHeight = $(window).height();
        var mainWrapHeight = $('.free_box').height();
        if(mainWrapHeight < windowHeight - 170){
            $('.free_box').height(windowHeight - 170 + 'px');
        }
    });

    //定时查询数据库状态  
    function queryOrderState(){
        HOME.ajaxHOMEFn({
            url: HOME.baseUrl.pay_status,
            type: 'POST',
            dataType: 'json',
            data: {
                order_sn: HOME.getQueryVariable('orderId')
            }
        }, function (res) {
            if(res.order_status == 1){  //未支付
                // window.location.href = HOME.locationVal.origin + "/login.html?page=info&id=" + HOME.getQueryVariable('id') + "&type=" + HOME.getQueryVariable('type') + "&typeAssort=" + HOME.getQueryVariable('typeAssort') + "&curId=" + HOME.getQueryVariable('curId')
            }else if(res.order_status == 5){    //已支付
                window.location.href = HOME.locationVal.origin + "/personal_center.html?id=" + HOME.getQueryVariable('id') + "&type=" + HOME.getQueryVariable('type') + "&typeAssort=" + HOME.getQueryVariable('typeAssort') + "&curId=" + HOME.getQueryVariable('curId')
            }else if(res.order_status == 10){   //支付失败
                $('.prompt_complete').show();
                $('.QR_code , .pay_complete').hide();
            }else if(res.order_status == 15){   //订单关闭
                $('.prompt_complete').show();
                $('.QR_code , .pay_complete').hide();
            }else if(res.order_status == 20){   //已退款
                $('.prompt_complete').show();
                $('.QR_code , .pay_complete').hide();
            }
        });
    }

    //页面超时处理
function overtime(){
    
    var m=0;
    var s=0;
    if(time_expire >= 0){
      m=Math.floor(time_expire/60%60);
      s=Math.floor(time_expire%60);
      time_expire = time_expire-1;
      //document.getElementById("minute").innerHTML = m;
      //document.getElementById("second").innerHTML = s;
      $("#minute").html(m);
      $("#second").html(s);
    }else{
        console.log('超时了')
    	window.self.location = 'http:www.baidu.com'
    }
}
    

 	/********************* 倒计时 start ****************************/
 var Alarm = function (second_time, countFunc, endFunc) {
    this.time = Math.floor(second_time); //时间
    this.countFunc = countFunc; //计时函数
    this.endFunc = endFunc; //结束函数
    this.flag = 't' + Date.parse(new Date()); //
   };
   Alarm.prototype.start = function () {
    var self = this;
    self.flag = setInterval(function () {
     if (self.time < 0) {
      clearInterval(self.flag);
      self.endFunc();
     } else {
      var minute, hour, day, second;
      day = Math.floor(self.time / 60 / 60 / 24) < 10 ? '0' + Math.floor(self.time / 60 / 60 / 24) : Math.floor(self.time / 60 / 60 / 24);
      hour = Math.floor(self.time / 60 / 60 % 24) < 10 ? '0' + Math.floor(self.time / 60 / 60 % 24) : Math.floor(self.time / 60 / 60 % 24);
      minute = Math.floor(self.time / 60 % 60) < 10 ? '0' + Math.floor(self.time / 60 % 60) : Math.floor(self.time / 60 % 60);
      second = Math.floor(self.time % 60) < 10 ? '0' + Math.floor(self.time % 60) : Math.floor(self.time % 60);
      //倒计时执行函数
      self.countFunc(second, minute, hour, day);
      self.time--;
  
     }
    }, 1000);
   }
  

   /********************* 倒计时 end ****************************/
   

   
})

